import 'package:flutter/material.dart';
import 'package:flutter_study_2/pages/HomePage.dart';
import 'package:flutter_study_2/pages/LoginPage.dart';

class AppRoutes {
  // 定义路由
  static final Map<String, WidgetBuilder> _routes = {
    '/': (context) => const HomePage(),
    'login': (context) => const LoginPage(),
  };

  // 路由生成和拦截器
  static Route<dynamic> generateRoute(RouteSettings settings) {
    // 这里可以添加路由拦截逻辑
    bool isAuthenticated = false; // 检查用户是否已登录

    if (settings.name != '/' && !isAuthenticated) {
      // 如果用户未登录且试图访问其他页面，重定向到登录页面
      return MaterialPageRoute(builder: (context) => const LoginPage());
    }

    // 返回对应的页面
    final builder = _routes[settings.name];
    if (builder != null) {
      return MaterialPageRoute(builder: builder);
    }

    // 返回一个默认页面
    return MaterialPageRoute(builder: (context) => const HomePage());
  }
}
